package com.yisa.module_glass.ui.adapters

import android.os.Build
import androidx.annotation.RequiresApi
import java.lang.Exception
import java.text.DateFormatSymbols
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*
import java.util.regex.Pattern

interface IFormatter {
    fun formatDateTime(captureTime: Long, pattern: String): String
}

object Formatter : IFormatter {

    override fun formatDateTime(captureTime: Long, pattern: String): String {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            FormatterV26.formatDateTime(captureTime, pattern)
        } else {
            FormatterLegacy.formatDateTime(captureTime, pattern)
        }
    }
}

@RequiresApi(Build.VERSION_CODES.O)
object FormatterV26 : IFormatter {

    override fun formatDateTime(captureTime: Long, pattern: String): String {
        val dateTimeFormatter = DateTimeFormatter.ofPattern(/*"MM-dd HH:mm"*/pattern)
        val localDateTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(captureTime), ZoneId.systemDefault())
        return localDateTime.format(dateTimeFormatter)
    }
}

object FormatterLegacy : IFormatter {
    override fun formatDateTime(captureTime: Long, pattern: String): String {
        val simpleDateFormat = SimpleDateFormat(/*"MM-dd HH:mm"*/pattern, Locale.getDefault())
        return try {
            simpleDateFormat.format(Date(captureTime))
        } catch (ex: Exception) {
            "解析失败"
        }
    }
}

